/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/10/24 19:55:22
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Admin
{
    /// <summary>
    /// articlelist1 的摘要说明
    /// </summary>
    public class Administrator1 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string falseStr = "{\"result\":false}";
            string trueStr = "{\"result\":true}";

            string operation = context.Request.Params["o"];
            string userName = context.Request.Params["userName"];
            if (string.IsNullOrEmpty(userName))
            {
                context.Response.Write(falseStr);
            }

            switch (operation)
            {
                case "0":
                    if (Delete(userName))
                    {
                        context.Response.Write(trueStr);
                    }
                    else
                    {
                        context.Response.Write(falseStr);
                    }
                    break;
                case "1":
                    if (SwitchLock(userName))
                    { 
                       context.Response.Write(trueStr);
                    }
                    else
                    {
                        context.Response.Write(falseStr);
                    }
                    break;
                default:
                    context.Response.Write(falseStr);
                    break;
            }
        }

        private bool Delete(string userName)
        {
            try
            {
                System.Web.Security.Membership.DeleteUser(userName);

                return true;
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                return false;
            }
        }

        private bool SwitchLock(string userName)
        {
            try
            {
                using (MembershipUserLogic logic = new MembershipUserLogic())
                {
                    TopUser user = logic.GetList(p => p.ApplicationName == System.Web.Security.Membership.ApplicationName && p.UserName == userName).FirstOrDefault();
                    if (user != null)
                    {
                        user.SetIsLockedOut(!user.IsLockedOut);
                        if (user.IsLockedOut)
                        {
                            user.SetLastLockoutDate(DateTime.Now);
                        }
                        logic.Save(user);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                return false;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}